/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "game_local.h"

/*-----------------------------------------------------------------------------
	Game main module :
-----------------------------------------------------------------------------*/

//
//	EGame::InitGameAPI
//
void EGame::InitGameAPI( void )
{
	lua_State *L = CoreLua();
	
	LUALIB_DECLARE	("game")
	LUALIB_METHOD	( runGame			)
	LUALIB_METHOD	( endGame			)
	LUALIB_METHOD	( purgeCachedData	)
	
	LUALIB_METHOD	( setView		)
	LUALIB_METHOD	( setProj		)
	LUALIB_METHOD	( addRendMesh	)
	LUALIB_METHOD	( addPhysMesh	)

	LUALIB_METHOD	( playCameraAnim )
	LUALIB_METHOD	( stopCameraAnim )
	
	LUALIB_METHOD	( spawnEntity	)
	LUALIB_METHOD	( killEntity	)
	
	LUALIB_METHOD	( traceRay		)
	LUALIB_METHOD	( traceBox		)
	LUALIB_METHOD	( overlap		)
	LUALIB_METHOD	( setGravity	)
	LUALIB_METHOD	( getGravity	)
	LUALIB_METHOD	( setWaving		)
	LUALIB_METHOD	( setAmbient	)

	LUALIB_METHOD	( getLocalCamera )
	LUALIB_REGISTER	( L )

	LUALIB_DECLARE	("rendmodel")
	LUALIB_METHOD	( playAnim		)
	LUALIB_REGISTER	( L )
	
	LUALIB_DECLARE	("ik")
	LUALIB_METHOD	( setIKTarget )
	LUALIB_REGISTER	( L )
	
	LUALIB_DECLARE	("entity")
	LUALIB_METHOD	( setPose		)	
	LUALIB_METHOD	( getPose		)	
	LUALIB_METHOD	( setupRigidBody	)
	LUALIB_METHOD	( setupRendModel	)
	LUALIB_METHOD	( setupCharacter	)
	LUALIB_METHOD	( setupShip			)
	LUALIB_METHOD	( addImpulse	)	
	LUALIB_METHOD	( addImpulse	)	
	LUALIB_METHOD	( getTable		)	
	LUALIB_METHOD	( setTable		)	
	LUALIB_REGISTER	( L )
	
	LUALIB_DECLARE	("character")
	LUALIB_METHOD	( jump			)	
	LUALIB_METHOD	( move			)	
	LUALIB_METHOD	( crouch		)	
	LUALIB_METHOD	( stand			)	
	LUALIB_REGISTER	( L )
	
	LUALIB_DECLARE	("shipmodel")
	LUALIB_METHOD	( addForce		)	
	LUALIB_METHOD	( setPose		)	
	LUALIB_METHOD	( getPose		)	
	LUALIB_METHOD	( getDynamics	)	
	LUALIB_METHOD	( updateForces	)	
	LUALIB_REGISTER	( L )
	
}


//
//	EGame::runGame
//
int EGame::runGame(lua_State *L)
{
	LUA_INTERFACE(L);
	
	EString level	=	lua.RequireString(1, "level name");
	
	self->RunGame( level );
	
	return 0;
}


//
//	EGame::endGame
//
int EGame::endGame(lua_State *L)
{
	LUA_INTERFACE(L);
	
	self->EndGame();
	
	return 0;
}


//
//	EGame::purgeCachedData
//
int EGame::purgeCachedData( lua_State *L )
{
	LUA_INTERFACE(L);
	
	ge()->ClearSceneCache();
	
	return 0;
}


/*-----------------------------------------------------------------------------
	View stuff :
-----------------------------------------------------------------------------*/

int EGame::setView( lua_State *L )
{
	LUA_INTERFACE(L);
	
	float x			=	lua.RequireNumber(1, "x-position");
	float y			=	lua.RequireNumber(2, "y-position");
	float z			=	lua.RequireNumber(3, "z-position");
	float yaw		=	lua.RequireNumber(4, "yaw (deg)");
	float pitch		=	lua.RequireNumber(5, "pitch (deg)");
	float roll		=	lua.RequireNumber(6, "roll (deg)");
	
	EQuaternion	z_up	=	EQuaternion::RotateAroundAxis( -PI/2.0f, EVector(0,0,1)) * EQuaternion::RotateAroundAxis(PI/2.0, EVector(1,0,0));
	EQuaternion	qrx		=	EQuaternion::RotateAroundAxis( EMath::Rad(roll),	EVector(1,0,0) );
	EQuaternion	qry		=	EQuaternion::RotateAroundAxis( EMath::Rad(pitch),	EVector(0,1,0) );
	EQuaternion	qrz		=	EQuaternion::RotateAroundAxis( EMath::Rad(yaw),		EVector(0,0,1) );
	EQuaternion	q		=	qrz * qry * qrx * z_up;
	EPoint		p		=	EPoint(x, y, z);
	
	self->fr_scene->SetView( p, q );
	self->dv_scene->SetView( p, q );

	//	set listener :	
	EVector view		= EVector(0, 0, -1).Rotate(q);
	EVector up			= EVector(0, 1,  0).Rotate(q);

	ss()->SetListener(p, view, up, EVector());

	if (self->camera_anim)
	{
		EPoint camera_pos;
		EQuaternion camera_orient;
		self->camera_anim->GetSampleAtTime(0, self->game_time/1000, camera_pos, camera_orient, true );

		EPoint ship_pose;
		EQuaternion ship_orient;
		self->entities[0]->GetPose(ship_pose, ship_orient);

		if (self->camera_lock) {		
			camera_pos		= camera_pos.Rotate(ship_orient) + EVector(ship_pose);
			camera_orient	= ship_orient*camera_orient;
		} else {
			EVector offset	= EVector(ship_pose.x, ship_pose.y, -2);
			
			camera_pos		= camera_pos.Rotate(ship_orient) + offset;

			float t_yaw, t_pitch, t_roll;
			EQuaternion temp_orient;

			ship_orient.ToAngles( t_yaw, t_pitch, t_roll);

			temp_orient = EQuaternion::FromAngles(t_yaw, 0.0f, 0.0f);

			camera_orient	= temp_orient*camera_orient;
		}

		self->fr_scene->SetView( camera_pos, camera_orient );
		self->dv_scene->SetView( camera_pos, camera_orient );		
	}
	
	
	return 0;
}


int EGame::setProj( lua_State *L )
{
	LUA_INTERFACE(L);
	
	float	fov		=	lua.RequireNumber(1, "fov (deg)");
	float	zn		=	lua.RequireNumber(2, "z near");
	float	zf		=	lua.RequireNumber(3, "z far");
	float	aspect	=	lua.RequireNumber(4, "aspect ratio");

	if (fov>=170) {
		fov = 170;
	}	

	uint w, h;
	::rs()->GetScreenSize(w,h);
	
	float	width	=	tan(EMath::Rad(fov/2)) * zf;
	float	height	=	width * (float)h / (float)w;

	self->fr_scene->SetProjection( zn, zf, width, height );
	self->dv_scene->SetProjection( zn, zf, width, height );
	
	return 0;
}

/*-----------------------------------------------------------------------------
	Camera animation stuff :
-----------------------------------------------------------------------------*/

//
//	EGame::playCameraAnim
//
int EGame::playCameraAnim( lua_State *L )
{
	LUA_INTERFACE(L);

	EString anim_file			= lua.RequireString(1, "anim file name");
	float	camera_fps			= lua.RequireNumber(2, "anim camera fps");
			self->camera_lock	= lua.RequireBoolean(3, "anim camera lock");

	LOGF("Loading camera animation track : %s", anim_file.CStr());
	self->camera_anim = ge()->LoadAnimTrack(anim_file);

	self->camera_anim->SetFPS(camera_fps);

	self->game_time = 0;

	return 0;
}


//
//	EGame::stopCameraAnim
//
int EGame::stopCameraAnim( lua_State *L )
{
	LUA_INTERFACE(L);

	self->camera_anim = NULL;

	return 0;
}


/*-----------------------------------------------------------------------------
	Level stuff :
-----------------------------------------------------------------------------*/

int EGame::addRendMesh( lua_State *L )
{
	LUA_INTERFACE(L);
	int n = lua_gettop(L);
	
	EString			path	=	lua.RequireString( 1, "fs/h path to node containing mesh" );
	
	IPxFREntity		rent	=	self->fr_scene->AddEntity();
	IPxSceneNode	node	=	ge()->LoadSceneNodeFromFile( path );
	IPxTriMesh		mesh	=	ge()->LoadMeshFromFile( path );
	
	EPoint	p;
	EQuaternion	q;
	node->GetGlobalPose( p, q );

	if (n!=1) {
		float x		= lua.RequireNumber(2, "x");
		float y		= lua.RequireNumber(3, "y");
		float z		= lua.RequireNumber(4, "z");
		float yaw	= lua.RequireNumber(5, "yaw");
		float pitch = lua.RequireNumber(6, "pitch");
		float roll	= lua.RequireNumber(7, "roll");
		p = EPoint(x,y,z);
		q = EQuaternion::FromAnglesRad(yaw, pitch, roll);
	}
	
	
	rent->SetMesh( mesh );
	rent->SetPose( p, q );

	self->level.rend_meshes.push_back( rent );
	
	return 0;
}


int EGame::addPhysMesh( lua_State *L )
{
	LUA_INTERFACE(L);
	int n = lua_gettop(L);

	EString			path	=	lua.RequireString( 1, "fs/h path to node containing mesh" );
	
	IPxPhysEntity	ent		=	phys()->AddEntity();
	IPxSceneNode	node	=	ge()->LoadSceneNodeFromFile( path );
	IPxTriMesh		mesh	=	ge()->LoadMeshFromFile( path );
	
	EPoint	p;
	EQuaternion	q;
	node->GetGlobalPose( p, q );

	if (n!=1) {
		float x		= lua.RequireNumber(2, "x");
		float y		= lua.RequireNumber(3, "y");
		float z		= lua.RequireNumber(4, "z");
		float yaw	= lua.RequireNumber(5, "yaw");
		float pitch = lua.RequireNumber(6, "pitch");
		float roll	= lua.RequireNumber(7, "roll");
		p = EPoint(x,y,z);
		q = EQuaternion::FromAnglesRad(yaw, pitch, roll);
	}
	
	ent->SetPose( p, q );
	ent->SetMesh( mesh );
	ent->SetMass(0);
	ent->SetStatic();
	ent->Spawn();

	self->level.phys_meshes.push_back( ent );

	return 0;
}


/*-----------------------------------------------------------------------------
	Entity stuff :
-----------------------------------------------------------------------------*/

//
//	EGame::spawnEntity
//
int EGame::spawnEntity( lua_State *L )
{
	LUA_INTERFACE(L);
	
	static uint begin = 0;
	
	for (uint i=0; i<GAME_MAX_ENTITIES; i++) {
		uint id = (begin + i) % GAME_MAX_ENTITIES;
		begin = id;
		
		if (!self->entities[id]) {
			self->entities[id] = new EGEntity( id );
			
			lua.SetStackGrow(1);
			lua_pushinteger(L, id);
			return 1;
		}
	}
	
	lua.Error("can not spawnEntity more entities");
	
	return 0;
}


//
//	EGame::killEntity
//
int EGame::killEntity( lua_State *L )
{
	LUA_INTERFACE(L);
	
	uint id = lua.RequireNumber(1, "game entity id");
	
	if (id>=GAME_MAX_ENTITIES) {
		lua.Error("id > %d", GAME_MAX_ENTITIES);
	}
	
	self->entities[id] = NULL;
	
	return 0;
}

/*-----------------------------------------------------------------------------
	Phys stuff :
-----------------------------------------------------------------------------*/

int EGame::traceRay( lua_State *L )
{
	LUA_INTERFACE(L);
	return 0;
}

int EGame::traceBox( lua_State *L )
{
	LUA_INTERFACE(L);
	return 0;
}

int EGame::overlap( lua_State *L )
{
	LUA_INTERFACE(L);
	return 0;
}


//
//	EGame::setGravity
//
int EGame::setGravity( lua_State *L )
{
	LUA_INTERFACE(L);
	
	float g	=	lua.RequireNumber(1, "gravity");
	
	self->level.gravity	=	g;
	phys()->SetGravity( EVector(0, 0, -g) );
	
	return 0;
}


//
//	EGame::getGravity
//
int EGame::getGravity( lua_State *L )
{
	LUA_INTERFACE(L);
	
	lua_pushnumber(L, self->level.gravity );
	
	lua.SetStackGrow(1);
	
	return 1;
}


int EGame::setWaving( lua_State *L )
{
	LUA_INTERFACE(L);

	int	b	=	lua_toboolean(L, 1);
	
	if (b) {
		self->waving	=	new EGWaving(L, 2);
	} else {
		self->waving	=	NULL;
	}
	
	return 0;	
}


int EGame::setAmbient( lua_State *L )
{
	LUA_INTERFACE(L);

	float	r	=	lua_tonumber(L, 1);
	float	g	=	lua_tonumber(L, 1);
	float	b	=	lua_tonumber(L, 1);
	
	::rs()->GetFRScene()->SetAmbientLevel( EColor( r, g, b, 0 ) );
	
	return 0;	
}

/*-----------------------------------------------------------------------------
	Entity stuff :
-----------------------------------------------------------------------------*/

//
//	EGame::checkEntityID
//
uint EGame::checkEntityID( lua_State *L, bool check_dead/* =true */ )
{
	LUA_INTERFACE(L);
	
	uint id = lua.RequireNumber(1, "game entity id");
	
	if (id>=GAME_MAX_ENTITIES) {
		lua.Error("entity ID > %d", GAME_MAX_ENTITIES);
	}
	
	if (!self->entities[ id ] && check_dead ) {
		lua.Error("entity ID=%d is dead");
	}
	
	return id;
}


EHrGEntity	EGame::getEntity( lua_State *L )
{
	uint id = checkEntityID(L);
	return self->entities[ id ];
}


//
//	EGame::setPose
//
int EGame::setPose( lua_State *L )
{
	LUA_INTERFACE(L);
	uint id	=	checkEntityID(L);
	
	EHrGEntity	ent	=	self->entities[id];
	
	float	x		=	lua.RequireNumber(2, "x");
	float	y		=	lua.RequireNumber(3, "y");
	float	z		=	lua.RequireNumber(4, "z");
	
	float	yaw		=	lua.RequireNumber(5, "yaw (deg)");
	float	pitch	=	lua.RequireNumber(6, "pitch (deg)");
	float	roll	=	lua.RequireNumber(7, "roll (deg)");
	
	ent->SetPose( EPoint(x,y,z), EQuaternion::FromAngles(yaw, pitch, roll) );
	return 0;
}


//
//	EGame::getPose
//
int EGame::getPose( lua_State *L )
{
	LUA_INTERFACE(L);
	uint id	=	checkEntityID(L);
	
	EHrGEntity	ent	=	self->entities[id];
	
	EPoint p;
	EQuaternion q;
	ent->GetPose(p, q);
	
	float yaw, pitch, roll;
	q.ToAngles( yaw, pitch, roll );
	
	lua_pushnumber(L, p.x);
	lua_pushnumber(L, p.y);
	lua_pushnumber(L, p.z);
	lua_pushnumber(L, yaw);
	lua_pushnumber(L, pitch);
	lua_pushnumber(L, roll);
	
	lua.SetStackGrow(6);
	return 6;
}


//
//	EGame::setupRigidBody
//
int EGame::setupRigidBody( lua_State *L )
{
	LUA_INTERFACE(L);
	uint id	=	checkEntityID(L);
	EHrGEntity	ent	=	self->entities[id];
	
	EString path	=	lua.RequireString(2, "path to physical model");
	float	mass	=	lua.RequireNumber(3, "mass");
	
	ent->SetupRigidbody( path, mass );
	
	return 0;
}


//
//	EGame::setupRendModel
//
int EGame::setupRendModel( lua_State *L )
{
	LUA_INTERFACE(L);
	uint id	=	checkEntityID(L);
	EHrGEntity	ent	=	self->entities[id];

	EString path	=	lua.RequireString(2, "path to view model");
	ent->SetupRendModel( path );
	
	return 0;
}


//
//	EGame::setupCharacter
//
int EGame::setupCharacter(lua_State *L)
{
	LUA_INTERFACE(L);
	uint id	=	checkEntityID(L);
	EHrGEntity	ent	=	self->entities[id];

	EGCharDesc	desc;
	
	desc.height			=	lua.RequireFieldNumber(2, "height"		);
	desc.width			=	lua.RequireFieldNumber(2, "width"		);

	ent->SetupCharacter( desc );
	
	return 0;
}


//
//	EGame::setupShip
//
int EGame::setupShip( lua_State *L )
{
	LUA_INTERFACE(L);
	uint id	=	checkEntityID(L);
	EHrGEntity	ent	=	self->entities[id];

	ent->SetupShip( L, 2 );
	
	return 0;
}


//
//	EGame::addImpulse
//
int EGame::addImpulse( lua_State *L )
{
	return 0;
}


//
//	EGame::getTable
//
int EGame::getTable( lua_State *L )
{
	LUA_INTERFACE(L);
	uint id	=	checkEntityID(L);
	EHrGEntity	ent	=	self->entities[id];
	ent->GetTable(L);
	
	lua.SetStackGrow(1);
	return 1;
}


//
//	EGame::setTable
//
int EGame::setTable( lua_State *L )
{
	LUA_INTERFACE(L);
	uint id	=	checkEntityID(L);
	EHrGEntity	ent	=	self->entities[id];
	
	ent->SetTable(L, 2);

	return 0;
}


//
//	EGame::isDead
//
int EGame::isDead( lua_State *L )
{
	uint id = checkEntityID(L, false);
	if (self->entities[ id ]) {
		lua_pushboolean(L, 1);
	} else {
		lua_pushboolean(L, 0);
	}
	return 1;
}


/*-----------------------------------------------------------------------------
	Entity stuff :
-----------------------------------------------------------------------------*/

//
//	EGame::jump
//
int EGame::jump( lua_State *L )
{
	return 0;
}


//
//	EGame::move
//
int EGame::move( lua_State *L )
{
	LUA_INTERFACE(L);
	EHrGEntity	ent	=	getEntity(L);
	
	float	x	=	lua.RequireNumber( 2, "x");
	float	y	=	lua.RequireNumber( 3, "y");
	float	z	=	lua.RequireNumber( 4, "z");
	
	bool floor, ceil, walls;
	ent->CharacterMove( EVector(x,y,z), floor, ceil, walls );
	
	lua.SetStackGrow(3);
	lua_pushboolean( L, floor );
	lua_pushboolean( L, ceil );
	lua_pushboolean( L, walls );

	return 3;
}


//
//	EGame::crouch
//
int EGame::crouch( lua_State *L )
{
	return 0;
}


//
//	EGame::stand
//
int EGame::stand( lua_State *L )
{
	return 0;
}


/*-----------------------------------------------------------------------------
	Entity stuff :
-----------------------------------------------------------------------------*/

//
//	EGame::playAnim
//
int EGame::playAnim( lua_State *L )
{
	LUA_INTERFACE(L);
	EHrGEntity	ent	=	getEntity(L);
	
	ent->rendmodel->PlayAnimation( lua.RequireString(2, "anim track"), lua.RequireNumber(3, "fps") );

	return 0;
}


//
//	EGame::setIKTarget
//
int EGame::setIKTarget( lua_State *L )
{
	return 0;
}


//
//	EGame::addForce
//  
int EGame::addForce( lua_State *L )
{
	LUA_INTERFACE(L);
	EHrGEntity	ent	=	getEntity(L);
	
	if (!ent->ship.ship) {
		return 0;
	}
	
	float	fx	=	lua.RequireNumber( 2, "fx");
	float	fy	=	lua.RequireNumber( 3, "fy");
	float	fz	=	lua.RequireNumber( 4, "fz");
	
	float	x	=	lua.RequireNumber( 5, "x");
	float	y	=	lua.RequireNumber( 6, "y");
	float	z	=	lua.RequireNumber( 7, "z");
	
	ent->ship.ship->ApplyForces( EVector(fx, fy, fz), EPoint(x,y,z) );
	
	return 0;
}


//
//	EGame::getLocalCamera
//  
int EGame::getLocalCamera( lua_State *L )
{
	LUA_INTERFACE(L);
	EHrGEntity	ent	=	getEntity(L);

	if (!ent->ship.ship) {
		return 0;
	}

	float l_x	=	lua.RequireNumber( 2, "local x");
	float l_y	=	lua.RequireNumber( 3, "local y");
	float l_z	=	lua.RequireNumber( 4, "local z");
	float yaw	=	lua.RequireNumber( 5, "local yaw");
	float pitch	=	lua.RequireNumber( 6, "local pitch");
	float roll	=	lua.RequireNumber( 7, "local roll");

	EPoint		local_pos		= EPoint(l_x, l_y, l_z);
	EQuaternion local_orient	= EQuaternion::FromAngles(yaw, pitch, roll);

	EPoint boat_pos;
	EQuaternion boat_orient;
	ent->ship.ship->GetPose(boat_pos, boat_orient);

	EPoint global_pos	= local_pos.Rotate(boat_orient) + EVector(boat_pos);
	EQuaternion global_orient = boat_orient*local_orient;

	global_orient.ToAngles(yaw, pitch, roll);

	lua_pushnumber(L, global_pos.x);
	lua_pushnumber(L, global_pos.y);
	lua_pushnumber(L, global_pos.z);
	lua_pushnumber(L, yaw);
	lua_pushnumber(L, pitch);
	lua_pushnumber(L, roll);
	
	lua.SetStackGrow(6);

	return 6;
}


//
//	EGame::getDynamics
//
int EGame::getDynamics( lua_State *L )
{
	LUA_INTERFACE(L);
	EHrGEntity	ent	=	getEntity(L);

	if (!ent->ship.ship) {
		return 0;
	}
	
	EGShip::ShipDynamics	sd	=	ent->ship.ship->GetShipDynamics();
	
	lua_newtable(L);
	
	lua_pushnumber(L,	sd.time);			lua_setfield(L, -2, "time");
	
	lua_pushnumber(L,	sd.force.x);		lua_setfield(L, -2, "force_x");
	lua_pushnumber(L,	sd.force.y);		lua_setfield(L, -2, "force_y");
	lua_pushnumber(L,	sd.force.z);		lua_setfield(L, -2, "force_z");
	
	lua_pushnumber(L,	sd.torque.x);		lua_setfield(L, -2, "torque_x");
	lua_pushnumber(L,	sd.torque.y);		lua_setfield(L, -2, "torque_y");
	lua_pushnumber(L,	sd.torque.z);		lua_setfield(L, -2, "torque_z");
	
	lua_pushnumber(L,	sd.position.x);		lua_setfield(L, -2, "position_x");
	lua_pushnumber(L,	sd.position.y);		lua_setfield(L, -2, "position_y");
	lua_pushnumber(L,	sd.position.z);		lua_setfield(L, -2, "position_z");

	lua_pushnumber(L,	sd.ship_width);		lua_setfield(L, -2, "ship_width");
	lua_pushnumber(L,	sd.ship_length);	lua_setfield(L, -2, "ship_length");
	lua_pushnumber(L,	sd.ship_mass);		lua_setfield(L, -2, "ship_mass");

	lua_pushnumber(L,	sd.wave_height);	lua_setfield(L, -2, "wave_height");
	float yaw, pitch, roll;	
	sd.orient.ToAngles(yaw, pitch, roll);
	
	lua_pushnumber(L,	yaw);	lua_setfield(L, -2, "yaw");
	lua_pushnumber(L,	pitch);	lua_setfield(L, -2, "pitch");
	lua_pushnumber(L,	roll);	lua_setfield(L, -2, "roll");

	lua.SetStackGrow(1);

	return 1;
}


//
//	EGame::updateForces
//
int EGame::updateForces( lua_State *L )
{
	LUA_INTERFACE(L);
	EHrGEntity	ent	=	getEntity(L);

	if (!ent->ship.ship) {
		return 0;
	}
	
	ent->ship.ship->UpdateForces();
	
	return 0;
}






